package com.google.code.gestion_de_conferences;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
	Button login_btn;
	AABDatabaseManager db ;
	Intent i;
	  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        db = new AABDatabaseManager(this);
        login_btn = (Button) findViewById(R.id.btn_login);                
        
        TextView link = (TextView) findViewById(R.id.link_to_register);
       
        //check if there is an admin user 
       	ArrayList<ArrayList<Object>> result = new ArrayList<ArrayList<Object>>();
		
		//get the admin user object from the db
		result = db.getRowAsArrayByPosition(6,"User", 1+"");
		
		//show register link if there is no admin users
		if(result.size() == 0)
		{			
			link.setText("New on Conferences? Register here");
			// Switching to Register screen
            i = new Intent(getApplicationContext(), RegisterActivity.class);
	        
		}
		else
		{
			link.setText("Forgot password?");
			// Switching to forgot password screen
            i = new Intent(getApplicationContext(), ForgotPasswordActivity.class);
		}
		// Listening to register new account/forget password link
		link.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View v) {
                
                startActivity(i);
            }
        });
		
		
        // Listening to register new account link
        login_btn.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View v) {
            	                        		
            	EditText reg_username = (EditText) findViewById(R.id.reg_username);
            	EditText reg_password = (EditText) findViewById(R.id.reg_password);
            	String username = reg_username.getText().toString();
            	String password = reg_password.getText().toString();
            	
            	TextView login_error = (TextView) findViewById(R.id.reg_login_error);
            	login_error.setText("");
            
            	ArrayList<String> rowArray = db.getRowAsArrayForLogin(username,password);
            	if(rowArray.size()==0)
            	{
            		login_error.setText("Wrong username/password combination");
            		
            	}
            	else
            	{
            		i = new Intent(getApplicationContext(), RoomsListActivity.class);
        	        startActivity(i);
            	}
            }
        });
    }
    
    /**
     * This will prevent back button of going to back page after logging out
     * 
     * @author Hyame Alameddine <hyame.alameddine@gmail.com> 
     */
    @Override
    public void onBackPressed() {
       
    	//showing message when clicking on the back button after logging out
    	 Toast.makeText(this, "Please login to access the application", Toast.LENGTH_LONG).show();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.no_login_menu, menu);              
        return true;
    }

    /**
	   * This function handles clicking any of the menu's items
	   * @author Rayan Zahab <differentruna@gmail.com> 
	   * @author Hyame Alameddine <hyame.alameddine@gmail.com>
	   * @param menu
	   */
	 @Override
	 public boolean onOptionsItemSelected(MenuItem item) 
	  {	  
		  switch (item.getItemId()) 
		  {
	          case R.id.help:     
	                AlertDialog alertDialog = new AlertDialog.Builder(this).create();
	                alertDialog.setTitle("Help!");
	                alertDialog.setMessage("Gestion de conference is an application helps you manage reservations, add rooms ,add users and reserve rooms to organize conferences !");
	                alertDialog.setIcon(R.drawable.icon);
	                alertDialog.show();
	                break;
	          case R.id.developers: 
	            	Toast.makeText(this, "Rayan,Hyam,Mostafa!", Toast.LENGTH_LONG).show();
	                break;
	            
	        }
	        return true;
	    }
}